package com.waves.task.listener;

import com.rabbitmq.client.Channel;
import com.waves.core.constant.MQConstant;
import com.waves.task.dao.TaskReceiveDao;
import com.waves.task.domain.entity.TaskReceive;
import com.waves.task.service.TaskService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 死信队列自动投递到普通任务 取消任务
 * @author dlw
 * @email
 */
@Service
public class TaskListener {

    @Resource
    private TaskService taskService;

    @Resource
    private TaskReceiveDao taskReceiveDao;

    /**
     * 取消任务
     *
     * @param taskReceive
     * @param channel
     * @param msg
     */
    @RabbitListener(queues = MQConstant.STOCK_COMMON_QUEUE)
    public void listener(TaskReceive taskReceive, Channel channel, Message msg) {
        // 0取消任务 1领取任务  2完成任务
        TaskReceive receive = taskReceiveDao.getByReceiverId(taskReceive.getTaskId(), taskReceive.getReceiverId());
        // 判断任务状态如果还是领取任务重，则自动取消
        if (receive.getStatus() == 1) {
            taskService.updateTaskStatus(0, taskReceive.getTaskId(), taskReceive.getReceiverId());
        }
    }


}
